<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:37
 */

namespace app\admin\model;

use think\facade\Session;

class Question extends \think\Model
{
    protected $table = 'sy_tk_cates';
    const Problem_TYPE = [
        1 => '单文本',
        2 => '多文本',
        3 => '富文本',
    ];

    /**
     * 获取单条
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function _findOne($id){
        return $this->where([['id', '=', $id]])->find();
    }

    
    /**
    * 通过名称获取信息
    */
    public function _findByName($name){
        return $this->where([['title', '=', $name]])->find();
    }
    
    //校验用户名是否存在
    public function _checkName($value){
        $data = $this->where([['cate_title', '=', $value]])->find();
        if(!empty($data)){
            $this->error = '已存在';
            return false;
        }
        return true;
    }
    

    /**
    * 校验是否被冻结
    */
    public function _checkStatus($id){
        $data = $this->where(array('id'=>$id, 'status'=>1))->find();
        if(empty($data)){
            $this->error = '已禁用';
            return false;
        }
        return true;
    }

    /**
     * 列表
     * @param array $condition
     * @param int $page_size
     * @param string $order_by
     * @return \think\Paginator
     */
    public function _list($condition = [], $page_size = PAGE_SIZE_ADMIN, $order_by = 'sort asc'){
        $obj = $this->alias('u')
            ->field('u.*,c.title')
            ->leftJoin('course_cate c','c.id=u.cate_id')
            ->order('id desc')
         ;
        $result = get_page($obj, $condition, $page_size);
       
        return $result;
    }

    /**
     * 添加
     * @param $post
     * @return bool
     */
    public function _create($post){
        $title = trim($post['cate_title']);
        $admin = $this->where([['cate_title', '=', $title]])->find();
        if($admin){
            $this->error = '已存在';
            return false;
        }
        $insert = [
            'cate_title' => $post['cate_title'],
            'effective_title' => $post['effective_title'],
            'create_at' => time(),
            'cate_id'   =>$post['cate_id'],
        ];
        if(!$this->insert($insert)){
            $this->error = '操作失败';
            return false;
        }
        return true;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function _remove($id){
        $result = $this->_findOne($id);
        if(!$result){
            $this->error = '无此';
            return false;
        }
        if(!$result->delete()){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }
    /**
     * 批量删除
     * @param $id
     * @return bool
     */
    public function _removeAll($id){
        $ret = $this->where([['id', 'in', $id]])->delete();
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        
        return true;
    }


    /**
     * 详情
     * @param $id
     * @return array|bool
     */
    public function _detail($id){
        $result = $this->_findOne($id)->toArray();
        if(!$result){
            $this->error = '无此数据';
            return false;
        }
        return $result;
    }


    /**
     * 修改
     * @param $post
     * @return bool
     */
    public function _modify($post){
        $result = $this->_findOne($post['id']);
        if(empty($result)){
            $this->error = '不存在';
            return false;
        }
        $data = $this->where([['cate_title', '=', trim($post['cate_title'])]])->find();
        if(!empty($data)){
            if($data->id != $result->id){
                $this->error = '已存在';
                return false;
            }
        }
        if(isset($post['cate_title'])) $result->cate_title = trim($post['cate_title']);
        if(isset($post['effective_title'])) $result->effective_title = $post['effective_title'];
        if($result->save() === false){
            $this->error = '操作失败';
            return false;
        }
        
        
        return true;
    }

    /**
    * 设置状态
    */
    public function _setStatus($request){
        $ff = $request['ff'];
        $id = $request['id'];
        if($ff == 1){
            $success = "停用成功";
            $error = "停用失败";
        }elseif($ff == -1){
            $success = "启用成功";
            $error = "启用失败";
        }
        
        
        $where = array('id'=>$id,'status'=>$ff);

        $data = $this->where($where)->find();
        if(empty($data)){
            $this->error = '该数据状态已改变';
            
            return false;
        }
        $data->status = -$ff;
        $ret = $data->save();
        if(!$ret){
            $this->error = $error;
            return false;
        }
        
        
        return true;
    }
    

    /**
    * 数据总条数
    */
    public function _count($where = []){
        return $this->alias('u')->where($where)->count();
    }

    /**
    * 获取条件
    */
    public function _where($request){
        $p = isset($request['page']) ? $request['page'] : 1;
        $start = isset($request['start']) ? $request['start'] : '';
        $end = isset($request['end']) ? $request['end'] : '';
        $type = isset($request['type']) ? $request['type'] : '';
        $cate_title = isset($request['cate_title']) ? $request['cate_title'] : '';
        
        $request['page'] = $p;
        $request['start'] = $start;
        $request['end'] = $end;
        $request['cate_title'] = $cate_title;
        $request['type'] = $type;
        
        $search = $request;

        $cate_title = htmlspecialchars(addslashes(str_replace(' ', '', $request['cate_title'])));
        $request['cate_title'] = str_replace('%', "\%", $cate_title);
        
        $tmp = $request;
        foreach($tmp as $k=>$v){
            if(empty($v)){
                unset($tmp[$k]);
            }
        }
        
        $where = '';
        foreach($tmp as $k=>$v){
            if($k == 'start'){
                $start = strtotime($v);
                $where .= "created >= $start and ";
            }elseif($k == 'end'){
                $end = strtotime($v) + 24*3600;
                $where .= "created <= $end and ";
            }elseif($k == 'cate_title'){
                $where .= "cate_title like '%$v%' and ";
            }elseif($k == 'type'){
                $where .= "type = $v and ";
            }
        }
        if(!empty($where)){
            $where = substr($where, 0, -4);
        }
        return array('search'=>$search,'where'=>$where);
    }
}